package main

import (
	"fmt"
	"math"
	"math/rand"
	"time"
)

//自动生成n位的随机数字
func CreatNumber(n int) int  {
	if n<0 {
		return 0
	}
	x:=1
	y:=8
	for i :=1;i<n;i++ {
		x*=10
		y*=10
		y+=9
	}
	rand.Seed(time.Now().UnixNano())
	num := x+rand.Intn(y)
	return num
}
//取出第m位
func SelectNum(mark,len,num int) int {
	ans:=0
	for i:=1;i<=mark;i++ {
		lenNum := 1*int(math.Pow(10, float64(len-1)))
		ans = num/lenNum
		num -= ans*lenNum
		len--
	}
	return ans
}

func Compare(guess,num,len int) string {
	var reply string
	var s string
	for i:=1;i<=len;i++ {
		a:=SelectNum(i,len,guess)
		s=""
		for j:=1;j<=len;j++ {
			b:=SelectNum(j,len,num)
			if a==b {
				if i==j {
					s ="A"
				}else {
					if s!="A" {
						s ="B"
					}
				}
			}
		}
		reply += s
	}
	return reply
}

func main() {
	fmt.Println("请输入预计输出几位位数的随机数：")
	var n int
	fmt.Scan(&n)
	num := CreatNumber(n)
	fmt.Println("随机数为：",num)

	t:= 0
	for  {
		t++
		fmt.Println("请输入你的第",t,"次猜测：")
		var guess int
		var reply string
		fmt.Scan(&guess)
		reply = Compare(guess,num,n)

		fmt.Println("您的正确标识为：",reply)

		if reply == "AAAA"{
			fmt.Println("恭喜你在第",t,"次答对了")
			break
		}
		reply = ""
	}
}
